package com.shyb.lingxian;

import android.content.Context;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.shyb.lingxian.Util.ScreenUtil;
import com.shyb.lingxian.Util.StatusUtil;
import com.shyb.lingxian.Util.ToastUtil;
import com.shyb.lingxian.base.BaseFragment;
import com.shyb.lingxian.gouWuChe.GouWuCheFragment;
import com.shyb.lingxian.home.HomeFragment;
import com.shyb.lingxian.mine.fragment.MineFragment;

import java.lang.reflect.Field;

import static com.shyb.lingxian.Util.ScreenUtil.statusbarheight;

public class MainActivity extends MainBarActivity  implements RadioGroup.OnCheckedChangeListener,View.OnClickListener  {

    private RadioButton[] radioButtons = new RadioButton[3];
    private String[]  fragmentTags={"main_home","main_gouwuche","main_mine"};
    public RadioGroup radioGroup;
    private int currentIndex = 0;
    private int toIndex = 0;
    private int PERMISSIONS_REQUEST_PHONE_STATE =105;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        View view=findViewById(R.id.root_main);
        FrameLayout.LayoutParams layoutParams= (FrameLayout.LayoutParams) view.getLayoutParams();
        layoutParams.setMargins(0,-getStatusBarHeight(this),0,0);
        view.setLayoutParams(layoutParams);
        StatusUtil.setTranslucent(this,true);

        bindViews();
        radioGroup.setOnCheckedChangeListener(this);
        transteFragment(fragmentTags[0], fragmentTags[0], HomeFragment.class);
        radioButtons[0].setChecked(true);
    }

    private long exitTime = 0;
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
            if ((System.currentTimeMillis() - exitTime) > 2000) {
                ToastUtil.showToast(this,"再按一次退出程序");
                exitTime = System.currentTimeMillis();
            } else {
                finish();
                System.exit(0);
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    private void bindViews() {
        radioGroup = (RadioGroup)findViewById(R.id.main_raidogroup);
        radioButtons[0]=(RadioButton)findViewById(R.id.rb_home);
        radioButtons[1]=(RadioButton)findViewById(R.id.rb_gouwuche);
        radioButtons[2]=(RadioButton)findViewById(R.id.rb_mine);
        findViewById(R.id.rl_home).setOnClickListener(this);
        findViewById(R.id.rl_approve).setOnClickListener(this);
        findViewById(R.id.rl_personal).setOnClickListener(this);

    }

    public void transteFragment(String oldTag,String newTag, Class<? extends BaseFragment> class1){
        if (!isFinishing()){  //确保fragment.show 的时候activity存在；

            FragmentManager manager = getSupportFragmentManager();
            Fragment fragment = manager.findFragmentByTag(newTag);
            Fragment oldFragment = manager.findFragmentByTag(oldTag);
            FragmentTransaction transaction = getSupportFragmentManager()
                    .beginTransaction();
            if (fragment == null && class1 != null) {
                try {
                    fragment = class1.newInstance();
                    if (oldFragment == null) {
                        transaction.add(R.id.main_home, fragment, newTag)
                                .commitAllowingStateLoss();
                    } else {
                        transaction.hide(oldFragment)
                                .add(R.id.main_home, fragment, newTag)
                                .commitAllowingStateLoss();
                    }
                } catch (InstantiationException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            } else {
                if (oldFragment == null) {
                    transaction.show(fragment).commitAllowingStateLoss();
                } else {
                    transaction.hide(oldFragment).show(fragment)
                            .commitAllowingStateLoss();
                }
            }
            currentIndex = toIndex;
        }
    }



    @Override
    public void onClick(View v) {
        Class<? extends BaseFragment> fragmentClass = null;
        radioButtons[currentIndex].setChecked(false);
        switch (v.getId()) {
            case R.id.rl_home: {
                toIndex = 0;
                fragmentClass = HomeFragment.class;
                radioButtons[0].setChecked(true);
                radioButtons[1].setChecked(false);
                radioButtons[2].setChecked(false);
            }

            break;
            case R.id.rl_approve: {
                toIndex = 1;
                fragmentClass = GouWuCheFragment.class;
                radioButtons[0].setChecked(false);
                radioButtons[1].setChecked(true);
                radioButtons[2].setChecked(false);
            }
            break;
            case R.id.rl_personal: {
                toIndex = 2;
                fragmentClass = MineFragment.class;
                radioButtons[0].setChecked(false);
                radioButtons[1].setChecked(false);
                radioButtons[2].setChecked(true);

            }
            break;

        }
        if (currentIndex == toIndex)
            return;

        if (fragmentClass != null) {
            transteFragment(fragmentTags[currentIndex], fragmentTags[toIndex], fragmentClass);
        }
    }

    public static int getStatusBarHeight(Context context) {
        if (statusbarheight == 0) {
            try {
                Class<?> c = Class.forName("com.android.internal.R$dimen");
                Object o = c.newInstance();
                Field field = c.getField("status_bar_height");
                int x = (Integer) field.get(o);
                statusbarheight = context.getResources().getDimensionPixelSize(x);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        if (statusbarheight == 0) {
            statusbarheight = ScreenUtil.dip2px(25);
        }
        return statusbarheight;
    }

    @Override
    public void onCheckedChanged(RadioGroup radioGroup, int i) {

    }
}
